<?php

namespace App\Http\Controllers\admin;
use DB;
use Config;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\adver\addRequest;
use App\Http\Controllers\Controller;

class AdverController extends Controller
{
     /**
	 *广告模块添加
	 * /admin/recommend/add
	 * /admin/recommend/insert
     */
    //编辑添加
    public function getAdd() {

    	//跳转到添加页面 
    	return view("adver.add");
    }

    //执行添加
    public function postInsert(addRequest $request) {
    	
    	//图片信息处理
    	if ($request -> hasFile("pic")) {
    		$path = config::get("app.upload_path");	//获取文件上传的路径
    		
    		//给图片重命名
    		do {
    			$picname = date("YmdHis").rand(10000,9999).$request->file('pic')->getClientOriginalName();
    		}while(file_exists($path.$picname));
    	}

    	//过滤多余字段
    	$data = $request -> except(["_token", "pic", "editorValue"]);

    	//注入处理后的字段
    	$data["pic"] = $picname;
    	$data["des"] = $request -> input("editorValue");
    	dd($data);
    	//执行插入
    	$res = DB::table("gs_recommend") -> insert($data);

    	//解析结果集
    	if ($res) {
    		//成功则执行图片上传和缩放
    		//执行上传	
            $request->file('pic') -> move($path,$picname); 

            //设置图片缩放必要参数
            $imgDir = $path.$picname;

            //缩放图片
            imgR($imgDir,156,150,"m_");
			imgR($imgDir,50,50,"s_");

			//页面跳转并提示
			return redirect("/admin/recommend/index") -> with("success", "添加成功！");
    	} else {

    		//失败返回并提示
    		return back() -> with("error", "添加失败！");
    	}
    }

    /**
	 * 推荐模块浏览
	 * /admin/recommend/index
     */
    public function getIndex(Request $request) {
    	
    	//获取图片上传路径
		$path = trim(config::get("app.upload_path"),".");

		//查询数据
		$res = DB::table("gs_recommend") -> where(function($query)use($request) {
			
			//判断是否存在搜索
	    	if ($request -> input("keywords")) {
	    		$query -> where("order", $request -> input("keywords"))
	    		-> orWhere("id", $request -> input("keywords"))
	    		-> orWhere("name", "like", "%".$request -> input("keywords")."%");
	    	} 
		}) -> paginate($request -> input("num",10));
		
		//返回到浏览界面
		return view("recommend.index", ["list" => $res, "path" => $path, "request" => $request -> all()]);
    }

    /**
	*banner是否前台显示修改
	* /admin/banner/delete  浏览页面
    */
    public function getStatus($status,$id) {
    	
    	//根据当前状态确定修改过后的状态
    	if($status == 1) {
    		$num = 0;
    	} else {
    		$num = 1;
    	}
    	
    	//执行修改
    	$res = DB::table("gs_recommend") -> where("id", $id) -> update(["status" => $num]);

    	//解析结果集
   		if ($res){

   			//返回并提示
   			return redirect("/admin/recommend/index") -> with("success", "修改成功！");
   		} else {
   			return back() -> with("error", "修改失败！");
   		}
    }

    /**
	*banner删除
	* /admin/banner/delete  浏览页面
    */
    public function getDelete($id,$pic) {
		
		//删除该模板下的商品列表
		$res1 = DB::table("rec_gs") -> where("tid", "$id") -> delete();

		//解析结果集
		if ($res1) {	//删除成功,继续删除版块
			
			//获取图片上传路径
			$path = config::get("app.upload_path");
	    	
	    	//删除数据库内容
	    	$res = DB::table("gs_recommend") -> where("id", $id) -> delete();

	    	//解析结果集
	    	if ($res) {

	    		//成功则删除图片
	    		unlink($path.$pic);
	    		unlink($path."s_".$pic);
	    		unlink($path."m_".$pic);


	    		//跳转并告知用户
	    		return redirect("/admin/recommend/index") -> with("success", "删除成功！");
	    	} else {

	    		//失败则返回
	    		return back() -> with("error", "删除失败！");
	    	}
   		} else {	//删除失败返回并提示
   			return back() -> with("error", "删除失败！");
   		}
    }
    
}
